package com.ym.fc.controller;

import com.ym.fc.bean.User;
import com.ym.fc.dto.UserDTO;
import com.ym.fc.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Controller
 *
 * @author yanm
 * @date 2021/1/7 15:31
 **/
@RestController
@RequestMapping("v1/api")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserController {

    private final UserService userService;

    @PostMapping("listUsers")
    public UserDTO list(User user) {
        List<User> select = userService.select(user);
        UserDTO userDTO = new UserDTO();
        userDTO.setUsers(select);
        return userDTO;
    }

    @PostMapping("loginData")
    public ModelAndView loginPost(@RequestBody User user) {
        User login = userService.login(user);
        /**
         *登陆成功
         **/
        if (login != null) {
            return new ModelAndView("");
        }
        /**
         *登陆失败
         **/
        return new ModelAndView("");
    }

    @PostMapping("registerData")
    public ModelAndView registerPost(@RequestBody User user) {
        int add = userService.add(user);
        if (add == 1) {
            return new ModelAndView("");
        }
        return new ModelAndView("");
    }
}
